<?php

/**
 * Description of users
 *
 * @author Lugol Metayer
 */
class Owners extends Controller{

		function index(){
		$ownerslist = $this->owners_info->retrieve_owners();

		$data = array();

		$data["owners"] = $ownerslist;
		$session_user = $this->session->userdata('username');
		$session_permission = $this->session->userdata('permission');
		$data['user'] = $session_user;
		$data['permission'] = $session_permission;
    $data['title'] = "L & R Vet Clinic";

    $this->load->view('owner/index', $data);
		

		}
		function frompet(){
		$session_user = $this->session->userdata('username');
		$session_permission = $this->session->userdata('permission');
		$data['user'] = $session_user;
		$data['permission'] = $session_permission;
    $data['title'] = "L & R vet clinic";
    
    $this->load->view('owner/option', $data);
    

		}
		function sort(){
		$ownerslist = $this->owners_info->orderby_owners();

		$data = array();

		$data["owners"] = $ownerslist;
$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
		$data['title'] = "L & R Owners";
                 
                 $this->load->view('owner/index', $data);
                 
		
		}
		function detail($wherestring){
		$ownerslist = $this->owners_info->retrieve_details($wherestring);

		$data = array();
		$data["num_of_pets"] = count($ownerslist);
		$data["contact"] = $ownerslist['0'];
		$data["pets"] = $ownerslist;

		$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
		$data['title'] = "L & R Owners";
                 
                 $this->load->view('owner/detail', $data);
                 
		}
		function add(){

				if (isset($_POST['fname']) && isset($_POST['lname']))
					{
							$this->form_validation->set_rules('fname', 'FirstName', 'required|trim|xss_clean|alpha');
							$this->form_validation->set_rules('lname', 'LastName', 'required|trim|xss_clean|alpha');
							$this->form_validation->set_rules('address', 'Address', 'required');
							$this->form_validation->set_rules('city', 'City', 'required');
							$this->form_validation->set_rules('state', 'State', 'required');
							$this->form_validation->set_rules('zip', 'Address', 'required');
							$this->form_validation->set_rules('email', 'Email', 'required|valid_email');

							if ($this->form_validation->run() == FALSE)
							{
									$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
								$data['title'] = "L & R Owners";
                                                                 
                                                                 $this->load->view('owner/addowner');
                                                                
							}
							else
							{

									$first = $_POST['fname'];
									$last = $_POST['lname'];
									$address = $_POST['address'];
									$city = $_POST['city'];
									$state = $_POST['state'];
									$zip = $_POST['zip'];
									$phoneH = $_POST['phoneHome'];
									$phoneC = $_POST['phoneCell'];
									$phoneW = $_POST['phoneWork'];
									$email = $_POST['email'];
									$notes = $_POST['notes'];

									$this->owners_info->insert_owners($first,$last,$address,$city,$state,$zip,$phoneH, $phoneC, $phoneW,$email, $notes);

									$ownerslist = $this->owners_info->retrieve_id($first,$last);

									foreach($ownerslist as $key => $object){
									$id = $object->owner_id;
									}
									$segments = array('pets', 'add', $id);

									redirect(site_url($segments), 'refresh');
							}
					}
					else{
$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
					$data['title'] = "L & R Owners";
                                         
                                         $this->load->view('owner/addowner');
                                        
					}
		}
		function update($wherestring){

		if (isset($_POST['fname']) && isset($_POST['lname']))
					{
							$this->form_validation->set_rules('fname', 'FirstName', 'required|trim|xss_clean|alpha');
							$this->form_validation->set_rules('lname', 'LastName', 'required|trim|xss_clean|alpha');
							$this->form_validation->set_rules('address', 'Address', 'required');
							$this->form_validation->set_rules('city', 'City', 'required');
							$this->form_validation->set_rules('state', 'State', 'required');
							$this->form_validation->set_rules('zip', 'Address', 'required');
							$this->form_validation->set_rules('email', 'Email', 'required|valid_email');

							if ($this->form_validation->run() == FALSE)
							{
								$ownerslist = $this->owners_info->retrieve_details($wherestring);

								$data = array();
								$data["contact"] = $ownerslist['0'];
								$data["pets"] = $ownerslist;

$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
								$data['title'] = "Blue Dolphin Owners";
                                                                 $this->load->view('header', $data);
                                                                 $this->load->view('menu');
                                                                 $this->load->view('owner/edit', $data);
                                                                 $this->load->view('footer');
								
							}
							else
							{
									$first = $_POST['fname'];
									$last = $_POST['lname'];
									$address = $_POST['address'];
									$city = $_POST['city'];
									$state = $_POST['state'];
									$zip = $_POST['zip'];
									$phoneH = $_POST['phoneHome'];
									$phoneC = $_POST['phoneCell'];
									$phoneW = $_POST['phoneWork'];
									$email = $_POST['email'];
									$notes = $_POST['notes'];
									$id =  $wherestring;

									$this->owners_info->update_owners($id,$first,$last,$address,$city,$state,$zip,$phoneH, $phoneC, $phoneW,$email, $notes );

									$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
								$data['title'] = "L & R Owners";
                                                                 
                                                                 $this->load->view('owner/success');
                                                                 
							}
					}
					else{
					$ownerslist = $this->owners_info->retrieve_details($wherestring);

					$data = array();
					$data["contact"] = $ownerslist['0'];
					$data["pets"] = $ownerslist;
					$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
                                         $data['title'] = "L & R Owners";
					
                                         $this->load->view('owner/edit', $data);
                                         
					}

		}
		function delete($wherestring){
		$this->owners_info->delete_owners($wherestring);

$session_user = $this->session->userdata('username');
								 $session_permission = $this->session->userdata('permission');
								$data['user'] = $session_user;
								$data['permission'] = $session_permission;
		 $data['title'] = "L & R Owners";
                 
                 $this->load->view('owner/success');
                 

		}
}
//*EOF*//
